package v1

import (
	"gin-example/models"
	"gin-example/pkg/e"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"log"
	"net/http"
	"strconv"
)

func GetBMUOverview(c *gin.Context) {
	data := make(map[string]interface{})
	bmuId := c.Query("bmuId")
	tmp, _ := strconv.Atoi(bmuId)
	code := e.ERROR_NOT_EXIST
	if models.ExistBMUById(tmp) {
		code = e.SUCCESS
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": data,
	})
}

func GetBMUsForUser(c *gin.Context) {
	data := make(map[string]interface{})
	userId := c.Query("userId")
	tmp, _ := strconv.Atoi(userId)
	code := e.ERROR_NOT_EXIST
	if models.ExistUserByID(tmp) {
		code = e.SUCCESS
		data["bmus"] = models.GetBMUsForUser(tmp)
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": data,
	})
}

func GetBMUsByStationId(c *gin.Context) {
	data := make(map[string]interface{})
	stationId := c.Query("stationId")
	valid1 := validation.Validation{}
	valid1.Required(stationId, "stationId").Message("储能站ID不能为空")
	code := e.INVALID_PARAMS
	if !valid1.HasErrors() {
		code = e.SUCCESS
		tmp1, _ := strconv.Atoi(stationId)
		data["bmus"] = models.GetBMUsByStationId(tmp1)
	} else {
		for _, err := range valid1.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": data,
	})
}

func GetBMUsByBSUId(c *gin.Context) {
	data := make(map[string]interface{})
	bsuId := c.Query("bsuId")
	valid1 := validation.Validation{}
	valid1.Required(bsuId, "bsuId").Message("BSU的ID不能为空")
	code := e.INVALID_PARAMS
	if !valid1.HasErrors() {
		code = e.SUCCESS
		tmp1, _ := strconv.Atoi(bsuId)
		data["bmus"] = models.GetBMUsByBSUId(tmp1)
	} else {
		for _, err := range valid1.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": data,
	})
}

func GetBMUsByBCUId(c *gin.Context) {
	data := make(map[string]interface{})
	bcuId := c.Query("bcuId")
	valid1 := validation.Validation{}
	valid1.Required(bcuId, "bcuId").Message("BCU的ID不能为空")
	code := e.INVALID_PARAMS
	if !valid1.HasErrors() {
		code = e.SUCCESS
		tmp1, _ := strconv.Atoi(bcuId)
		data["bmus"] = models.GetBMUsByBCUId(tmp1)
	} else {
		for _, err := range valid1.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": data,
	})
}

func AddBMU(c *gin.Context) {
	json := make(map[string]string)
	_ = c.BindJSON(&json)

	userId := json["userId"]
	id := json["id"]
	stationId := json["stationId"]
	bsuId := json["bsuId"]
	bcuId := json["bcuId"]
	batteryNum := json["batteryNum"]
	batteryType := json["batteryType"]
	valid1 := validation.Validation{}
	valid1.Required(stationId, "stationId").Message("储能站ID不能为空")
	valid1.Required(id, "id").Message("BMU的ID不能为空")
	valid1.Required(userId, "userId").Message("操作人员的ID不能为空")
	valid1.Required(batteryNum, "batteryNum").Message("电池数量不能为空")
	valid1.Required(batteryType, "batteryType").Message("电池种类不能为空")
	valid2 := validation.Validation{}
	valid2.Required(bsuId, "bsuId").Message("BSU_ID可以为空")
	valid3 := validation.Validation{}
	valid3.Required(bcuId, "bcuId").Message("BCU_ID可以为空")
	code := e.INVALID_PARAMS
	if !valid1.HasErrors() {
		code = e.ERROR_NO_AUTHORITY
		tmpUserId, _ := strconv.Atoi(userId)
		tmpBMUId, _ := strconv.Atoi(id)
		tmpStationId, _ := strconv.Atoi(stationId)
		tmpBatteryNum, _ := strconv.Atoi(batteryNum)
		tmpBatteryType, _ := strconv.Atoi(batteryType)
		if models.CheckAuthority(tmpUserId, tmpStationId) {
			code = e.ERROR_NOT_EXIST
			if !valid2.HasErrors() && !valid3.HasErrors() {
				tmpBSUId, _ := strconv.Atoi(bsuId)
				tmpBCUId, _ := strconv.Atoi(bcuId)
				if models.ExistBSUById(tmpBSUId) && models.ExistBCUById(tmpBCUId) {
					code = e.SUCCESS
					models.AddBMUComplete(tmpBMUId, tmpStationId, tmpBSUId, tmpBCUId, tmpBatteryNum, tmpBatteryType)
				}
			} else if valid3.HasErrors() && !valid2.HasErrors() {
				tmpBSUId, _ := strconv.Atoi(bsuId)
				if models.ExistBSUById(tmpBSUId) {
					code = e.SUCCESS
					models.AddBMUWithBSU(tmpBMUId, tmpStationId, tmpBSUId, tmpBatteryNum, tmpBatteryType)
				}
			} else if valid2.HasErrors() && !valid3.HasErrors() {
				tmpBCUId, _ := strconv.Atoi(bcuId)
				if models.ExistBCUById(tmpBCUId) {
					code = e.SUCCESS
					models.AddBMUWithBCU(tmpBMUId, tmpStationId, tmpBCUId, tmpBatteryNum, tmpBatteryType)
				}
			} else {
				code = e.SUCCESS
				models.AddBMU(tmpBMUId, tmpStationId, tmpBatteryNum, tmpBatteryType)
			}
		}
	} else {
		for _, err := range valid1.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": make(map[string]string),
	})
}

func UpdateBMU(c *gin.Context) {
	id := com.StrTo(c.Param("id")).MustInt()
	code := e.INVALID_PARAMS

	if models.ExistBMUById(id) {
		code = e.SUCCESS
		json := make(map[string]interface{})
		_ = c.BindJSON(&json)
		stationId := json["stationId"]
		bsuId := json["bsuId"]
		bcuId := json["bcuId"]
		batteryNum := json["batteryNum"]
		batteryType := json["batteryType"]
		electricity := json["electricity"]
		status := json["status"]
		if stationId != nil {
			tmp, _ := strconv.Atoi(stationId.(string))
			json["stationId"] = tmp
		}
		if bsuId != nil {
			tmp, _ := strconv.Atoi(bsuId.(string))
			json["bsuId"] = tmp
		}
		if bcuId != nil {
			tmp, _ := strconv.Atoi(bcuId.(string))
			json["bcuId"] = tmp
		}
		if batteryNum != nil {
			tmp, _ := strconv.Atoi(batteryNum.(string))
			json["batteryNum"] = tmp
		}
		if batteryType != nil {
			tmp, _ := strconv.Atoi(batteryType.(string))
			json["batteryType"] = tmp
		}
		if electricity != nil {
			tmp, _ := strconv.ParseFloat(electricity.(string), 64)
			json["electricity"] = tmp
		}
		if status != nil {
			tmp, _ := strconv.Atoi(status.(string))
			json["status"] = tmp
		}
		models.UpdateBMU(id, json)
	} else {
		code = e.ERROR_NOT_EXIST
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": make(map[string]string),
	})
}

func DeleteBMU(c *gin.Context) {
	id := com.StrTo(c.Param("id")).MustInt()
	code := e.INVALID_PARAMS
	if models.ExistBMUById(id) {
		models.DeleteBMU(id)
		code = e.SUCCESS
	} else {
		code = e.ERROR_NOT_EXIST
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": make(map[string]string),
	})
}
